using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace Edge.Common.ComponentModel
{
    public class NavigationItem
    {
      
        private string name;
        private string seo;
        private string urlPath;
        private NavigationItem[] childrens;
        private bool current;

        [XmlAttribute("name")]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        [XmlAttribute("urlPath")]
        public string UrlPath
        {
            get { return urlPath; }
            set { urlPath = value; }
        }

        [XmlArray("subitems")]
        [XmlArrayItem("item", typeof(NavigationItem))]
        public NavigationItem[] Childrens
        {
            get { return childrens; }
            set { childrens = value; }
        }

        public string Seo
        {
            get { return seo; }
            set { seo = value; }
        }

        public bool Current
        {
            get { return current; }
            set { current = value; }
        }

        public NavigationItem()
        {
        }

        public NavigationItem(string name, string urlPath, NavigationItem[] childrens,  bool current)
        {
            this.name = name;
            this.urlPath = urlPath;
            this.childrens = childrens;
            this.current = current;
        }

        public NavigationItem(string name, string urlPath)
        {
            this.name = name;
            this.urlPath = urlPath;
            this.current = false;
        }

        public NavigationItem(string seo, string name, string urlPath)
        {
            this.seo = seo;
            this.name = name;
            this.urlPath = urlPath;
            this.current = false;
        }


        

    }

    [XmlRoot("menu")]
    public class NavigationMenu
    {

        private NavigationItem[] _variables;

        [XmlArray("items")]
        [XmlArrayItem("item", typeof(NavigationItem))]
        public NavigationItem[] Items
        {
            get { return _variables; }
            set { _variables = value; }
        }
    }

    public class MenuGenerator
    {
        private XmlSerializer _ser = new XmlSerializer(typeof(NavigationMenu));

        public NavigationMenu Create(string xmlFile)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlFile);
            XmlNode section = doc.DocumentElement.ParentNode;
            NavigationMenu ni = (NavigationMenu)_ser.Deserialize(new XmlNodeReader(section));
            return ni;

        }

        public ICollection<NavigationItem> GetCollection(string xmlFile)
        {
            return GetCollection(xmlFile, null);
        }

        public ICollection<NavigationItem> GetCollection(string xmlFile, string currentPath)
        {
            NavigationMenu nm = Create(xmlFile);
            ICollection<NavigationItem> niColl = new List<NavigationItem>();
            for (int i = 0; i < nm.Items.Length; i++)
            {
                if (nm.Items[i].UrlPath.ToLower() == currentPath.ToLower())
                    nm.Items[i].Current = true;
                niColl.Add(nm.Items[i]);
            }
            return niColl;
        }

    }
}
